const nATMV = require('express').Router()
nATMV.route('/wx')
    .post(async (req, res, next) => {
        if (req.jsonData.MsgType[0] == 'text') {
            const { jsonData } = req
            const mongoose = require('mongoose')
            //发送图片
            if (jsonData.Content[0].trim() == '抗原检测') {
                //在创建新的会话前先验证有无未完成的会话
                const NAT = mongoose.model('nat', require('../../../model/nATSchema'))
                const unfinishNAT = await NAT.findOne({ userID: req.user.userID, isFinish: false })
                const User = mongoose.model('user', require('../../../model/userSchema'))

                //再验证有无当日的会话，如果有就删除
                await NAT.deleteMany({ userID: req.user.userID, isFinish: true, date: require('../../../tools/getDate')('today') })

                if (!unfinishNAT) {
                    // 添加抗原检测会话
                    const natSession = {
                        sessionType: 'NAT',
                        createDate: require('../../../tools/getDate')('today'),
                        sessionID: jsonData.MsgId[0]
                    }
                    //在用户数据中添加会话
                    await User.updateOne({ userID: req.user.userID }, { $push: { sessions: natSession } })
                    //创建抗原会话
                    const nat = {
                        sessionID: natSession.sessionID,
                        userID: req.user.userID,
                        pic: '',
                        isFinish: false,
                        address: '',
                        hasAddress: false,
                        picID: '',
                        hasPic: false,
                        date: natSession.createDate
                    }
                    const newNAT = new NAT(nat)

                    //防止因网络延迟产生的重复数据
                    NAT.findOne({ sessionID: natSession.sessionID }).then(isExist => {
                        if (!isExist) newNAT.save()
                    })

                    //提示用户输入楼号和房号
                    const msg = '请输入您所在的楼号和房号'
                    const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], msg)
                    res.end(xmlSendData)

                } else {
                    if (!unfinishNAT.hasAddress) {
                        //提示用户输入楼号和房号
                        const msg = '请输入您所在的楼号和房号'
                        const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], msg)
                        res.end(xmlSendData)
                    } else if (!unfinishNAT.hasPic) {
                        //提示用户上传照片
                        const msg = '请添加抗原照片'
                        const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], msg)
                        res.end(xmlSendData)
                    }
                }
            } else {
                //如果有未完成的NAT检测时收到的文字信息作为房间号使用
                const NAT = mongoose.model('nat', require('../../../model/nATSchema'))
                const unfinishNAT = await NAT.findOne({ userID: req.user.userID, isFinish: false })
                if (unfinishNAT) {
                    await NAT.updateOne({ userID: req.user.userID, isFinish: false }, { $set: { address: jsonData.Content[0], hasAddress: true } })

                    if (!unfinishNAT.hasPic) {
                        //提示用户上传照片
                        const msg = '请添加抗原照片'
                        const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], msg)
                        res.end(xmlSendData)
                    } else {
                        //提示用户完成操作
                        const msg = '感谢使用，您的抗原信息已添加完成！'
                        const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], msg)
                        await NAT.updateOne({ userID: jsonData.FromUserName[0], isFinish: false }, {
                            $set: {
                                isFinish: true
                            }
                        })
                        res.end(xmlSendData)
                    }

                } else {
                    next()

                }
            }
        }
    })
module.exports = nATMV